﻿using FlyingEye.ComeServices;
using Volo.Abp.DependencyInjection;

namespace FlyingEye.DeviceMonitor
{
    /// <summary>
    /// 管理员群
    /// </summary>
    public class AdminGroup
    {
        public const string Secret = "SECb38733e71346cb9353315eeef15d8f919c90d2d224d17bcb89cbe0c4cb9ccf42";
        public const string AccessToken = "52046048273d0909e3f9025f4c46de0e601caea47539b56199055f0a45cc0cc7";
    }

    /// <summary>
    /// 失败的任务状态
    /// </summary>
    public class FailureTaskStatus
    {
        public DateTimeOffset NextTime { get; set; }

        public FailureTaskStatus(DateTimeOffset nextTime)
        {
            NextTime = nextTime;
        }
    }

    /// <summary>
    /// 任务通知服务
    /// </summary>
    public class TaskNotificationService : ITransientDependency
    {
        // 这里是一个隐患，如果任务过度更换，会造成内存膨胀
        private static readonly ObjectStatusManager _manager = new ObjectStatusManager();

        private readonly ComeNotificationService _comeNotificationService;

        public TaskNotificationService(ComeNotificationService comeNotificationService)
        {
            _comeNotificationService = comeNotificationService;
        }

        /// <summary>
        /// 通知失败的监视任务到管理员组
        /// </summary>
        public async Task NotifyFailureTaskToAdminGroupAsync(Guid taskId, string taskName, string description)
        {
            if (_manager.CanAccessObject(taskId, (int)TimeSpan.FromHours(12).TotalMilliseconds))
            {
                // 通知信息到管理员群
                var result = await _comeNotificationService.NotifyGroupAsync(
                AdminGroup.AccessToken,
                AdminGroup.Secret,
                "异常监视任务",
                $"# 异常监视任务\n\n**任务ID**：{taskId}\n\n**任务名称**：{taskName}\n\n**描述**：{description}\n\n**UTC时间**：{DateTime.UtcNow}");
            }
        }
    }
}
